
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.ScrollPaneLayout;

import calendar_ex.Appointment;
import calendar_ex.CalendarDate;
import calendar_ex.CalendarEx;
import calendar_ex.CalendarTime;

class DayViewPanel extends ObservingJPanel {
	
	JButton[] buttons;
	CalendarEx calendarmodel;
	CalendarController controller;

	public DayViewPanel(CalendarEx model, CalendarController controller) {

		super(model, controller);
		this.update(); 

	}

	@Override
	public void update() {
		this.removeAll();

		JPanel grid = new JPanel();

		JPanel left = new JPanel(); 
		left.setLayout(new FlowLayout(FlowLayout.LEFT, 0,0));
		left.add(grid); 
		JScrollPane sp = new JScrollPane(left);

		this.setLayout(new GridLayout(1,1));
		sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		gbl.setConstraints(sp, c);
		grid.setLayout(gbl);
        grid.setMaximumSize(new Dimension(100, 300));
		
		String[] times = {"All Day","12am","1","2","3","4","5","6am","7","8","9","10","11",
				"12pm","1","2","3","4","5","6pm","7","8","9","10","11"};

		for (int i = 0; i<times.length; i++) {
			c.gridx = 0;
			c.gridy = i;
			JButton b = new JButton(times[i]);
			b.setEnabled(false);
			b.setPreferredSize(new Dimension(80,20));
			grid.add(b, c);
		}
		
		
		CalendarDate startDate = new CalendarDate(calendarModel.getViewDate().getDay(),calendarModel.getViewDate().getMonth(),calendarModel.getViewDate().getYear());
		List<Appointment> apps = calendarModel.getAppointmentsBetweenDates(startDate,startDate);


		int xmax = 1;
		ArrayList<Point> filled = new ArrayList<Point>();
		buttons = new JButton[apps.size()];
		

		for (int count = 0; count < apps.size(); count++) {
			Appointment a = apps.get(count);
			CalendarTime st = a.start_time;
			CalendarTime et = a.end_time;

			//check if all day appointment

			int ycoord = st.hr+1;
			int yheight = et.hr - st.hr;
			int xcoord;
			boolean clash = false;
			int i;

			//find xcoord (so that appointments at same time do not overlap)
			for (i = 1; i <= xmax; i++) {
				clash = false;
				for (int j = ycoord; j <= ycoord + yheight; j++) {
					if (filled.contains(new Point(i,j))) {
						clash = true;
						break;
					}
				}
				
				if (clash == false)
					break;
			}

			if (clash == true) {
				xcoord = ++xmax;
			}
			else
				xcoord = i;
			
			for (int j = ycoord; j <= ycoord + yheight; j++){
				Point p = new Point(xcoord, j);
				filled.add(p);
			}
			
			c.fill = GridBagConstraints.BOTH;
			c.gridx = xcoord;
			c.gridy = ycoord;
			c.gridwidth = 1;
			c.gridheight = yheight;

			JButton b = new JButton(a.name + " " + a.description);
            
            int width = 190;
            b.setToolTipText(a.name + " " + a.description);
			b.addActionListener(new EventButtonListener(a));
            b.setMargin(new Insets(0, 0, 0, 0));
            
            buttons[count] = b;
            
            for (i = 0; i < xmax; i++) {
                buttons[count-i].setPreferredSize(new Dimension(width/xmax, 40));
                buttons[count-i].setMaximumSize(new Dimension(width/xmax, 40));
            }
            
			grid.add(b, c);
		}
		

		this.add(sp);

	}
	
	class EventButtonListener implements ActionListener {
		Appointment a;
		
		public EventButtonListener(Appointment a) {
			this.a = a;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			calendarModel.setCurrentAppointment(a);
            calendarController.changeView(IS3Calendar.View.EVENT);
		}
		
		
	}

}
